package com.admin.system.config;

import org.apache.ibatis.cache.Cache;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.locks.ReadWriteLock;

/**
 * @author caixiaofeng
 * @version 1.0
 * @Description: <<>>
 * @company 捷创智能技术有限公司
 * @create 2021-04-16 15:20
 */
//@Component
public class RedisCache implements Cache {
    private final String id;

    @Override
    public ReadWriteLock getReadWriteLock() {
        return null;
    }

    public RedisCache(String id) {
        this.id = id;
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public void putObject(Object key, Object value) {
        RedisTemplate redisTemplate = getRedisTemplate();
        redisTemplate.opsForValue().set(key.toString(), value);
    }

    @Override
    public Object getObject(Object key) {
        RedisTemplate redisTemplate = getRedisTemplate();
        return redisTemplate.opsForValue().get(key.toString());
    }

    @Override
    public Object removeObject(Object key) {
        RedisTemplate redisTemplate = getRedisTemplate();
        return redisTemplate.delete(key.toString());
    }

    @Override
    public void clear() {
        RedisTemplate redisTemplate = getRedisTemplate();
         redisTemplate.delete(this.id);
    }

    @Override
    public int getSize() {
        return 0;
    }

    private RedisTemplate getRedisTemplate(){
        return ApplicationContextConfig.getBean(RedisTemplate.class);
    }

}
